﻿using System.Collections.Generic;
using Core.Library;

namespace DataObject.Comment
{
    public class CommentController
    {
        /***************************************
        = Back End
        -------------------------------------- */
        public static void Comment_Update_Status(int commentId, int status)
        {
            CommentDAL.Comment_Update_Status(commentId, status);
        }

        public static void Comment_Delete(int commentId)
        {
            CommentDAL.Comment_Delete(commentId);
        }

        public static CommentObject Comment_GetById(int commentId)
        {
            return ObjectHelper.FillObject<CommentObject>(CommentDAL.Comment_GetById(commentId));
        }

        public static int Comment_GetStatistic(int status)
        {
            return CommentDAL.Comment_GetStatistic(status);
        }       

        public static List<CommentObject> Comment_GetListSeach(int typeId, int status, int pageIndex, int pageSize, ref int totalRecords)
        {
            // Nếu filter rỗng thì lấy tất cả bình luận
            var filter = string.Empty;

            switch (status)
            {
                case (int)CommentType.IsWaitingForPublish:
                    filter += string.Format("Status = {0}", status);
                    break;
                case (int)CommentType.IsPublished:
                    filter += string.Format("Status = {0}", status);
                    break;
            }

            if (typeId > 0)
                filter += string.Format(status > 0 ? " AND TypeId = {0}" : "TypeId = {0}", typeId);    

            totalRecords = Action.GetListPagingCount("Comment", filter);
            return ObjectHelper.FillCollection<CommentObject>(
                    Action.Reader_GetListPaging("Comment", "CreatedOn DESC", filter, pageIndex, pageSize));
        }

        /***************************************
        = Front End
        -------------------------------------- */
        public static int Comment_Create(CommentObject o)
        {
            return CommentDAL.Comment_Create(o);
        }

        public static List<CommentObject> Comment_GetByParentId(int id, int parentId, int status)
        {
            return ObjectHelper.FillCollection<CommentObject>(CommentDAL.Comment_GetByParentId(id, parentId, status));
        }
        
        public static void Comment_BuilTree(int id, List<CommentObject> arrayList, int level, int parentId, int status)
        {
            var comments = Comment_GetByParentId(id, parentId, status);

            foreach (var c in comments)
            {                
                arrayList.Add(c);

                level++;
                Comment_BuilTree(id, arrayList, level, c.CommentId, status);
                level--;
            }
        }

        public static List<CommentObject> PaginatedCollection(List<CommentObject> comments, int pageIndex, int pageSize)
        {
            var paginatedCollection = new List<CommentObject>();

            int windowFloor = pageIndex == 1 ? -1 : ((pageSize * pageIndex) - 1) - pageSize >= 0 ? ((pageSize * pageIndex) - 1) - pageSize : 0;

            int windowCeiling = (pageSize * pageIndex) - 1;

            foreach (var i in comments)
            {
                int a = comments.IndexOf(i);
                int b = a;
                if (comments.IndexOf(i) <= windowCeiling && comments.IndexOf(i) > windowFloor)
                    paginatedCollection.Add(i);
            }

            return paginatedCollection;
        }

        public static List<CommentObject> PaginationComment(int id, int typeId, int pageIndex, int pageSize, ref int totalRecords)
        {
            var lstComment = new List<CommentObject>();
            Comment_BuilTree(id, lstComment, 0, 0, (int)CommentType.IsPublished);

            totalRecords = lstComment.Count;

            var lstTemp = PaginatedCollection(lstComment, pageIndex, pageSize);
            return lstTemp;
        }
    }
}
